package com.py.credit.service.impl;

import com.py.credit.dao.DataMapCollectionDao;
import com.py.credit.service.DataMapCollectionService;
import com.py.credit.utils.NumberUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据归集大屏serviceImpl
 */
@Service
public class DataMapCollectionServiceImpl implements DataMapCollectionService{

    @Autowired
    private DataMapCollectionDao dataMapCollectionDao;

    @Override
    public List<LinkedHashMap> getOfficeUploadTop10() {
        return dataMapCollectionDao.selectOfficeUploadTop10();
    }

    @Override
    public Map<String,Object> getOfficeRateOfIncrease() {
        Map<String,Object> data=new HashMap<String,Object>();
        List<LinkedHashMap> deptList=dataMapCollectionDao.selectDeptList();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar cur = Calendar.getInstance();
        Calendar up = Calendar.getInstance();
        cur.setTime(date);
        up.setTime(date);
        up.add(Calendar.MONTH, -1);

        if(null!=deptList && deptList.size()!=0){
            for (LinkedHashMap dept:deptList
                    ) {
                //前6个月增长率
                cur.add(Calendar.MONTH, -5);
                up.add(Calendar.MONTH, -5);
                String curMonth = sdf.format(cur.getTime());
                String upMonth = sdf.format(up.getTime());
                LinkedHashMap value=new LinkedHashMap();
                List<LinkedHashMap> dataList=new ArrayList<LinkedHashMap>();
                LinkedHashMap map=dataMapCollectionDao.selectOfficeRateOfIncrease((Long) dept.get("deptId"),curMonth,upMonth);
                value.put("gjNum",map.get("gjNum"));
                value.put("rate",map.get("rate"));
                value.put("date",curMonth);
                dataList.add(value);

                for (int i=0;i<5;i++){
                    cur.add(Calendar.MONTH, 1);
                    up.add(Calendar.MONTH, 1);
                    curMonth = sdf.format(cur.getTime());
                    upMonth = sdf.format(up.getTime());
                    map=dataMapCollectionDao.selectOfficeRateOfIncrease((Long) dept.get("deptId"),curMonth,upMonth);
                    value=new LinkedHashMap();
                    value.put("gjNum",map.get("gjNum"));
                    value.put("rate",map.get("rate"));
                    value.put("date",curMonth);
                    dataList.add(value);
                }

                data.put((String) dept.get("officeName"),dataList);
            }
        }else {
            //初始化数据
            cur.add(Calendar.MONTH, -5);
            String curMonth = sdf.format(cur.getTime());
            LinkedHashMap value=new LinkedHashMap();
            List<LinkedHashMap> dataList=new ArrayList<LinkedHashMap>();
            value.put("gjNum",0);
            value.put("rate",0);
            value.put("date",curMonth);
            dataList.add(value);

            for (int i=0;i<5;i++){
                cur.add(Calendar.MONTH, 1);
                curMonth = sdf.format(cur.getTime());
                value=new LinkedHashMap();
                value.put("gjNum",0);
                value.put("rate",0);
                value.put("date",curMonth);
                dataList.add(value);
            }

            data.put("--",dataList);
        }

        return data;
    }

    @Override
    public List<LinkedHashMap> getAreaRateOfIncrease() {
    	List<LinkedHashMap> all=new ArrayList<LinkedHashMap>();
    	List<LinkedHashMap> listCodes=dataMapCollectionDao.queryAllAreaCodes();
    	for(int i=0;i<listCodes.size();i++){
			LinkedHashMap l=new LinkedHashMap();
			l=dataMapCollectionDao.selectAreaRateOfIncrease(this.getAreaId(String.valueOf(listCodes.get(i).get("code"))));
			l.put("qybm", listCodes.get(i).get("code"));
			l.put("qymc", listCodes.get(i).get("name"));
			all.add(l);
    	}
        return all;
    }

    @Override
    public List<LinkedHashMap> getAreaUpload() {
        //查询一级区域
        List<LinkedHashMap> data=new ArrayList<LinkedHashMap>();
        List<LinkedHashMap> areaList=dataMapCollectionDao.selectAreaList();

        //归集数量数组
        long[] gjNumArr=new long[areaList.size()];

        //地区名称数组
        String[] areaNameArr=new String[areaList.size()];

        //把数据存入数组中
        for(int i=0;i<areaList.size();i++){
            gjNumArr[i]=dataMapCollectionDao.selectAreaUpload((Long) areaList.get(i).get("areaId"));
            areaNameArr[i]= (String) areaList.get(i).get("areaName");
        }

        //冒泡排序，从动大到小排列
        long t1=0L;
        String t2="";
        for(int i=0;i<gjNumArr.length-1;i++)
        {
            for(int j=0;j<gjNumArr.length-1-i;j++)
            {
                if(gjNumArr[j]<gjNumArr[j+1])
                {
                    t1=gjNumArr[j];
                    gjNumArr[j]=gjNumArr[j+1];
                    gjNumArr[j+1]=t1;

                    t2=areaNameArr[j];
                    areaNameArr[j]=areaNameArr[j+1];
                    areaNameArr[j+1]=t2;
                }
            }
        }

        for (int i=0;i<gjNumArr.length;i++){
        	if(i==5) {
                break;
            }
            LinkedHashMap map=new LinkedHashMap<>();
            map.put("areaName",areaNameArr[i]);
            map.put("gjNum",gjNumArr[i]);
            data.add(map);
        }

        return data;
    }

    @Override
    public List<Map> getAreaUploadByMonth() {
    	List<Map> all=new ArrayList<Map>();
    	List<LinkedHashMap> listCodes=dataMapCollectionDao.queryAllAreaCodes();
    	/*for(int i=0;i<listCodes.size();i++){
    		all.add(dataMapCollectionDao.selectAreaRateOfIncrease(this.getAreaId((String)listCodes.get(i).get("name"))));
    	}
        return all;*/
    	
    	
    	for(int j=0;j<listCodes.size();j++){
    		Map areaData=new HashMap();
    		List<LinkedHashMap> dataList=new ArrayList<LinkedHashMap>();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, -11);
            String curMonth = sdf.format(c.getTime());
            LinkedHashMap map = new LinkedHashMap();
            map.put("gjDate",curMonth);
            map.put("gjNum",dataMapCollectionDao.selectAreaUploadByMonth(this.getAreaId(String.valueOf(listCodes.get(j).get("code"))),curMonth));
            dataList.add(map);

            for(int i=0;i<11;i++){
                map = new LinkedHashMap();
                c.add(Calendar.MONTH, 1);
                curMonth = sdf.format(c.getTime());
                map.put("gjDate",curMonth);
                map.put("gjNum",dataMapCollectionDao.selectAreaUploadByMonth(this.getAreaId(String.valueOf(listCodes.get(j).get("code"))),curMonth));
                dataList.add(map);
            }
          //如果没有数据的话给一个空数据
            if(null==dataList || dataList.size()==0){
            	dataList=new ArrayList<LinkedHashMap>();
                SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM");
                Calendar c1 = Calendar.getInstance();
                LinkedHashMap startMap=new LinkedHashMap();
                c1.add(Calendar.MONTH, -11);
                startMap.put("gjNum",0);
                startMap.put("gjDate",sdf1.format(c1.getTime()));
                dataList.add(startMap);
                for(int i=0;i<11;i++){
                    LinkedHashMap linkedHashMap=new LinkedHashMap();
                    c1.add(Calendar.MONTH, 1);
                    linkedHashMap.put("gjNum",0);
                    linkedHashMap.put("gjDate",sdf.format(c1.getTime()));
                    dataList.add(linkedHashMap);
                }
            }
            areaData.put("dataList", dataList);
            areaData.put("qybm", String.valueOf(listCodes.get(j).get("code")));
            areaData.put("qymc", String.valueOf(listCodes.get(j).get("name")));
            all.add(areaData);
    	}
        return all;
    }

    @Override
    public List<LinkedHashMap> getDataSourceRateOfIncrease() {
        //查询主体库
        List<LinkedHashMap> dataBaseList=dataMapCollectionDao.selectdataBaseList();
        for (LinkedHashMap map:dataBaseList
             ) {
            LinkedHashMap data=dataMapCollectionDao.selectDataSourceRateOfIncrease(((Long) map.get("id")).intValue());
            map.put("gjNum",data.get("gjNum"));
            map.put("rate",data.get("rate"));
        }
        return dataBaseList;
    }

    @Override
    public List<LinkedHashMap> getDataSource() {
        //查询主体库
        List<LinkedHashMap> dataBaseList=dataMapCollectionDao.selectdataBaseList();
        for (LinkedHashMap map:dataBaseList
                ) {
            LinkedHashMap data=dataMapCollectionDao.selectDataSource(((Long) map.get("id")).intValue());
            map.put("gjNum",data.get("gjNum"));
        }
        return dataBaseList;
    }

    @Override
    public List<LinkedHashMap> getDataCollectType() {
        return dataMapCollectionDao.selectDataCollectType();
    }

    /**
     * 通过区域编码查询区域id
     * @param areaCode
     * @return
     */
    private long getAreaId(String areaCode){
        return dataMapCollectionDao.selectAreaIdByCode(areaCode);
    }

	@Override
	public List<LinkedHashMap> queryAllAreaCodes() {
		// TODO Auto-generated method stub
		return dataMapCollectionDao.queryAllAreaCodes();
	}

	@Override
	public LinkedHashMap collectionByMonth() {
		LinkedHashMap m=new LinkedHashMap();
		Double thisMonth=dataMapCollectionDao.collectionByThisMonth().doubleValue();
		Double lastMonth=dataMapCollectionDao.collectionByLastMonth().doubleValue();
		if(lastMonth==0) {
			m.put("countThisMonth", thisMonth);
			m.put("countLastMonth", lastMonth);
			m.put("rate", 1.0);
		}else {
			m.put("countThisMonth", thisMonth);
			m.put("countLastMonth", lastMonth);
			m.put("rate", NumberUtils.getDouble(2, (thisMonth-lastMonth)/lastMonth));
		}
		
		return m;
	}

	@Override
	public List<LinkedHashMap> collectionByRelateStatus() {
		// TODO Auto-generated method stub
		return dataMapCollectionDao.collectionByRelateStatus();
	}

	@Override
	public List<LinkedHashMap> collectionByTarget(String type) {
		// TODO Auto-generated method stub
		return dataMapCollectionDao.collectionByTarget(type);
	}
}
